package cool.mtc.minghe.web.controller.sys.permission;

import cool.mtc.core.page.PageData;
import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.permission.dto.PermissionDTO;
import cool.mtc.minghe.data.model.sys.permission.form.PermissionForm;
import cool.mtc.minghe.data.model.sys.permission.dto.PermissionListDTO;
import cool.mtc.minghe.data.model.sys.permission.query.PermissionQuery;
import cool.mtc.minghe.service.service.sys.permission.PermissionService;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 明河
 */
@Tag(name = "权限管理")
@RestController
@RequestMapping("api/sys/permission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PermissionController extends BaseController {
    private final PermissionService permissionService;

    @PreAuthorize("@ps.hasPermission('sys:permission:insert')")
    @Tag(name = "权限管理")
    @Operation(summary = "新增", description = "权限：sys:permission:insert")
    @PostMapping("insert")
    public long insert(@RequestBody @Validated PermissionForm form) {
        return permissionService.insert(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:permission:update')")
    @Tag(name = "权限管理")
    @Operation(summary = "更新", description = "权限：sys:permission:update")
    @PostMapping("update")
    public void update(@RequestBody @Validated PermissionForm form) {
        permissionService.update(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:permission:delete')")
    @Tag(name = "权限管理")
    @Operation(summary = "删除", description = "权限：sys:permission:delete")
    @PostMapping("delete")
    public void delete(@RequestBody IdForm form) {
        permissionService.delete(form.getId());
    }

    @PreAuthorize("@ps.hasPermission('sys:permission:read')")
    @Tag(name = "权限管理")
    @Operation(summary = "分页列表", description = "权限：sys:permission:read")
    @GetMapping("list/page")
    public PageData<PermissionListDTO> pageList(PermissionQuery query) {
        return permissionService.pageList(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:permission:read')")
    @Tag(name = "权限管理")
    @Operation(summary = "详情", description = "权限：sys:permission:read")
    @GetMapping("info")
    public PermissionDTO info(Long id) {
        return permissionService.info(id);
    }
}
